﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Icoders.MarketTracker.Web.Lib.Command.Helper;
using Icoders.MarketTracker.Web.Lib.CommandHandler;
using StructureMap;

namespace Icoders.MarketTracker.Web.Lib.Command.Bus.Impl
{
    public class RunCommandInHost : IRunCommandInHost
    {
        private IResolveCommand _resolveCommand;
        public RunCommandInHost(IResolveCommand resolveCommand)
        {
            _resolveCommand = resolveCommand;
        }
        public void RunCommand(ICommand command)
        {
            //_log.InfoFormat("Running {0} : {1} --- {2}", criLocal.CommandType.ToString(), criLocal.CommandId, criLocal.JsonCommand);
            ResolveCommandItem rci = _resolveCommand.Get(command);
            object ch = ObjectFactory.GetInstance(rci.CommandHandlerContract);
            Type u = typeof(ICommandHandler<>);
            Type c = u.MakeGenericType(rci.Command);
            c.GetMethod("Execute").Invoke(ch, new object[] { command });
        }
    }
}
